﻿using App.Dal;
using App.Model;
using App.Untiys;
using Sunny.UI;
using Sunny.UI.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Win.Dal;

namespace App.Desk
{
    public partial class FrmLogin : UILoginForm
    {
        public FrmLogin()
        {
            InitializeComponent();
            OnLogin += FrmLogin_OnLogin;
            lblTitle.Text = Config.Company;
            lblSubText.Text = Config.SampingerName;
        }

        private bool FrmLogin_OnLogin(string userName, string password)
        {
            List<decimal> decimals = new List<decimal>();
            for(int i=0;i<50;i++)
            {
                decimals.Add(new Random(DateTime.Now.Millisecond).ToNextDouble(0.8, 1.5).ToDecimalEx());
                Thread.Sleep(50);
            }

            //if (password == "KD8858110!@#")
            //{
            //    FrmConfigSet frmConfigSet = new FrmConfigSet();
            //    frmConfigSet.ShowDialog();
            //    return false;
            //}
            if (string.IsNullOrEmpty(userName))
            {
                UIMessageTip.ShowWarning("请输入用户名");
                return false;
            }
            if (string.IsNullOrEmpty(password))
            {
                UIMessageTip.ShowWarning("请输入密码");
                return false;
            }
            var user = SysUserServices.Info(new SysUser { Acct = userName });
            if (user == null)
            {
                UIMessageTip.ShowWarning("用户名或者密码错误");
                return false;
            }
            if (user.Pwd == DesEncrypt.Encrypt(password))
            {
                if (!string.IsNullOrEmpty(user.Authority))
                    user.Authoritys = user.Authority.ToSplitArrayEx('|');
                if (user.IsAdmin == 1)
                    user.Authoritys = ConfigServices.GetNoDisableMenus().Select(m => m.Name).ToList();
                if (!user.Authoritys.IsNotNull())
                {
                    UIMessageTip.ShowWarning("该用户没有任何权限");
                    return false;
                }
                Config.CurrentUser = user;
                base.DialogResult = DialogResult.OK;
                base.Close();
                //FCustomMain frmMain = new FCustomMain();
                //this.Hide();
                // frmMain.ShowDialog();
            }
            else
            {
                UIMessageTip.ShowWarning("用户名或者密码错误");
                return false;
            }
            return false;
        }

    }
}
